<?php
define('MSG_HEAD', 1);

class Database {
    public static function connect() {
        $dsn = 'mysql:dbname=wiki;host=127.0.0.1';
        $user = 'root';
        $password = '';
        $dbh = null;
        try {
            $dbh = new PDO($dsn, $user, $password);
            $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        } catch (PDOException $e) {
            echo 'Connexion échouée : ' . $e->getMessage();
            exit(0);
        }
        return $dbh;
    }


}

class Messages {
    public $msgArray;

    public function  __construct($message=array(),$code=0) {
        if (is_array($message))
            $this->msgArray = $message;
        else {
            $this->msgArray = array();
            if($code!==0) array_push($this->msgArray,array('code'=>$code,'message'=>$message));
        }
    }

    public function pushMessage($message,$code) {
        array_push($this->msgArray,array('code'=>$code,'message'=>$message));
    }

    
    public function hasMessage() {
        return sizeof($this->msgArray)!==0;
    }

    public function pollMessageByCode($code) {
        $output = array();
        $new_msgArray = array();
        foreach ($this->msgArray as $item){
            if ($item['code']==$code)
                array_push($output, $item['message']);
            else
                array_push($new_msgArray, $item);
        }
        $this->msgArray = $new_msgArray;
        return count($output)==0?null:$output;
    }

    public function pollAllMessages () {
        $output = array();
        foreach ($this->msgArray as $item) array_push($output, $item['message']);
        $this->msgArray = array();
        return count($output)==0?null:$output;
    }

    public function getAllMessages () {
        $output = array();
        foreach ($this->msgArray as $item) array_push($output, $item['message']);
        return count($output)==0?null:$output;
    }
}

class SessionMsg { //use $_SEESION["message"] to transfert message between pages
    static public $messages;

    private static function build() { //ensure that $_SESSION has 'message' key
        if (!(isset($_SESSION['message']) && is_array($_SESSION['message']))) $_SESSION['message']=array();
       SessionMsg::$messages = new Messages($_SESSION['message']);
    }

    private static function refresh() {
        $_SESSION['message']= SessionMsg::$messages->msgArray;
    }

    private static function clear() {
        $_SESSION['message']=array();
        SessionMsg::build();
    }

    public static function hasMessage() {
        SessionMsg::build();
        return SessionMsg::$messages->hasMessage();
    }

    public static function pushMessage($message, $code) {
        SessionMsg::build();
        SessionMsg::$messages->pushMessage($message, $code);
        SessionMsg::refresh();
    }

    public static function pollMessageByCode($code) {
        SessionMsg::build();
        $output = SessionMsg::$messages->pollMessageByCode($code);
        SessionMsg::clear();
        return $output;
    }

    public static function pollAllMessages() {
        SessionMsg::build();
        return SessionMsg::$messages->pollAllMessages();
    }

    public static function getAllMessages() {
        return SessionMsg::$messages->getAllMessages();
    }
}



Class Client { //dealing information in $_POST and $_GET
    public static function hasCommand($cmd) {
        return array_key_exists($cmd,$_GET);
    }
    public static function dropCommand() { //refresh current script without command
        header('location:'.$_SERVER["SCRIPT_NAME"]);
        exit;
    }
    public static function showUri($uri) {
        header('location:'.$uri);
        exit;
    }
}



?>
